<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<div>
	<table id="goods-list-dg"></table>
</div>

<script type="text/javascript">
//定义商品列表插件，立即执行方法，只执行一次，所以不需要方法名！！
(function(){
	//初始化插件
	$('#goods-list-dg').datagrid({    
	    url:'/goods/list',    
	    columns:[[    
	        {field:'id',title:'id',hidden:true},    
	        {field:'title',title:'商品标题',width:150},    
	        {field:'status',title:'商品状态',width:100,
	        	formatter: function(value,row,index){
					if (value==1){
						return "<font color='green'>正常</font>";
					} else if(value==2){
						return "<font color='yellow'>下架</font>";
					}else if(value==3){
						return "<font color='red'>删除</font>";
					}
				}
	        },  
	        {field:'sellPoint',title:'卖点',width:150}, 
	        {field:'price',title:'价格',width:150},
	        {field:'number',title:'库存',width:150},
	        {field:'createTime',title:'创建时间',width:250,
	        	formatter: function(value,row,index){
					return new Date(value).format("yyyy-MM-dd hh:mm:ss.S");
				}	
	        },    
	        {field:'updateTime',title:'修改时间',width:250,
	        	formatter: function(value,row,index){
					return new Date(value).format("yyyy-MM-dd hh:mm:ss.S");
				}	
	        }
	    ]],
	    toolbar: [{
			iconCls: 'icon-add',
			text:'添 加',
			handler: function(){goods.openAddWindow();}
		}],
	    pagination:true,
	    pageSize:10,
	    pageList:[10,20,30],
	    rownumbers:true,
	    singleSelect:true
	});
}());

var goods = {};

goods.openAddWindow=function(){
	//创建一个添加窗口
	$('<div id="goods-list-add-dialog"></div>').dialog({    
	    title: '添加商品',    
	    width: 600,    
	    height: 500,    
	    closable:false,    
	    href: '/goods/add/',    
	    modal: true,
	    buttons:[{
			text:'添加',
			handler:function(){
				goods.add();
			}
		},{
			text:'关闭',
			handler:function(){
				console.info("...goods..");
				$('#goods-list-add-dialog').dialog('destroy');
			}
		}]
	});  
}

//修改
goods.edit=function(id,status){
	console.info(id);
	$.ajax({
	   type: "post",
	   async:false,
	   url: "/goods/edit",
	   data:{'id':id,'status':status},
	   success: function(msg){
	     if(msg.code == 200){
	    	//重新加载当前页
	    	$('#goods-list-dg').datagrid('reload');
	     }
	   }
	});
}

</script>